﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Money
{
    public delegate bool DeleteBackupHandler(BackupItem backupItem);

    public partial class DataBackupForm : Form
    {
        private List<BackupItem> _backupItems;
        private string _currentFile = null;
        public DeleteBackupHandler DeleteBackup { get; set; }

        public BackupItem BackupItem { get; private set; }

        public string CurrentFile
        {
            get
            {
                return _currentFile;
            }
            set
            {
                _currentFile = value;

                if (_currentFile != null)
                {
                    _showCurrentOnly.Enabled = true;
                    Text = " Data Backup Form : Current File - " + _currentFile;
                }
                else
                {
                    _showCurrentOnly.Enabled = false;
                    Text = " Data Backup Form";
                }
            }
        }

        public enum ActionType
        {
            Empty,
            Load,
            Restore
        }

        public List<BackupItem> BackupList 
        {
            get
            {
                return _backupItems;
            }
            set
            {
                _backupItems = value;
                UpdateViewableList();
            }
        }

        private void UpdateViewableList()
        {
            List<BackupItem> viewList;

            if (_showCurrentOnly.Checked == true)
            {
                var filterList = from backupItem in _backupItems where backupItem.OriginalFileName.Equals(_currentFile) select backupItem;
                viewList = filterList.ToList();
            }
            else
            {
                viewList = _backupItems;
            }

            _backupItemGrid.DataSource = null;
            _backupItemGrid.DataSource = viewList;
        }

        public ActionType Action { get; private set; }

        public DataBackupForm()
        {
            InitializeComponent();

            _backupItemGrid.AutoGenerateColumns = false;

            Action = DataBackupForm.ActionType.Empty;
        }

        private void _loadBackupButton_Click(object sender, EventArgs e)
        {
            BackupItem = _backupItemGrid.SelectedRows[0].DataBoundItem as BackupItem;

            if (BackupItem != null)
            {
                Action = ActionType.Load;
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void _backupItemGrid_SelectionChanged(object sender, EventArgs e)
        {
            if (_backupItemGrid.SelectedRows.Count == 1)
            {
                _loadBackupButton.Enabled = true;
                _restoreBackupButton.Enabled = true;
            }
            else
            {
                _loadBackupButton.Enabled = false;
                _restoreBackupButton.Enabled = false;
            }

            if (_backupItemGrid.SelectedRows.Count == 0)
            {
                _deleteBackupButton.Enabled = false;
            }
            else
            {
                _deleteBackupButton.Enabled = true;
            }
        }

        private void _restoreBackupButton_Click(object sender, EventArgs e)
        {
            BackupItem = _backupItemGrid.SelectedRows[0].DataBoundItem as BackupItem;

            if (BackupItem != null)
            {
                Action = ActionType.Restore;
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void _deleteBackupButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in _backupItemGrid.SelectedRows)
            {
                BackupItem item = row.DataBoundItem as BackupItem;

                if (item != null)
                {
                    if (DeleteBackup != null)
                    {
                        DeleteBackup(item);
                    }
                }
            }

            UpdateViewableList();
        }

        private void _showCurrentOnly_CheckedChanged(object sender, EventArgs e)
        {
            UpdateViewableList();
        }

        private void _backupItemGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            BackupItem backupItem = _backupItemGrid.Rows[e.RowIndex].DataBoundItem as BackupItem;

            if (backupItem != null)
            {
                if (backupItem.AutoSave)
                {
                    e.CellStyle.ForeColor = Color.Gray;
                }
            }

            if (CurrentFile != null)
            {
                if (CurrentFile.Equals(backupItem.OriginalFileName))
                {
                    e.CellStyle.ForeColor = Color.Blue;
                }
            }

            //if (CurrentFile != null)
            //{
            //    if (!CurrentFile.Equals(backupItem.OriginalFileName))
            //    {
            //        Font font = new Font(e.CellStyle.Font, FontStyle.Italic);

            //        e.CellStyle.Font = font;
            //    }
            //}
        }
    }
}
